package com.ygxsk.carhome.ui.evaluate;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.flyco.roundview.RoundTextView;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.tencent.mmkv.MMKV;
import com.trello.rxlifecycle4.android.ActivityEvent;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantApi;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.constant.ExtraKey;
import com.ygxsk.carhome.constant.ExtraValue;
import com.ygxsk.carhome.enumwrap.CallHistoryBusinessType;
import com.ygxsk.carhome.enumwrap.EvaluteCarEstimateType;
import com.ygxsk.carhome.enumwrap.EvaluteCarStatus;
import com.ygxsk.carhome.event.EventCarEvaluateMain;
import com.ygxsk.carhome.response.EvaluteCurrentItemBean;
import com.ygxsk.carhome.response.EvaluteCurrentItemOutSide;
import com.ygxsk.carhome.retrofit.AddLogUpLoadInfo;
import com.ygxsk.carhome.retrofit.ApiException;
import com.ygxsk.carhome.retrofit.ApiRequestSubListener;
import com.ygxsk.carhome.retrofit.ApiService;
import com.ygxsk.carhome.retrofit.ErrorConsumer;
import com.ygxsk.carhome.retrofit.NetworkManager;
import com.ygxsk.carhome.retrofit.ResponseTransformer;
import com.ygxsk.carhome.sys.Session;
import com.ygxsk.carhome.ui.ErrorActivity;
import com.ygxsk.carhome.ui.adapter.EvaluateListItemAdapter;
import com.ygxsk.carhome.utils.CommUtils;
import com.ygxsk.carhome.utils.ViewUitls;
import com.ygxsk.carhome.views.ClearEditTextNoClearSK;
import com.ygxsk.carhome.views.MyLinearLayoutManager;
import com.ygxsk.carhome.views.picker.NomalCustomBean;
import com.ygxsk.carhome.views.picker.NomalPickerDialogFragment;
import com.ygxsk.carhome.views.spinner.NiceSpinnersk;
import com.ygxsk.integrated.retrofit.Parameterkey;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-07-14  15:13
 * @Description:车辆评估的主页列表页面
 */
public class CarEvaluateMainActivity extends ErrorActivity {
    @BindView(R.id.refreshlayoutinevaluatelistactiviy)
    SmartRefreshLayout refreshLayoutInEvaluateListActiviy;

    @BindView(R.id.recyclerviewinevaluatelistactivity)
    RecyclerView recyclerViewInEvaluateListActivity;

    //添加 车辆评估
    @BindView(R.id.roundbuttoaddnewevaluate)
    RoundTextView roundButtoAddNewEvaluate;

    //选择类型
    @BindView(R.id.nice_spinner_evaluate_type)
    NiceSpinnersk nice_spinner_evaluate_type;

    //状态的分配
    @BindView(R.id.nice_spinner_state_item)
    NiceSpinnersk nice_spinner_state_item;

    //搜索的逻辑；
    @BindView(R.id.et_searchincustomsmall)
    ClearEditTextNoClearSK et_searchInCustomSmall;

    //搜索按钮的布局
    @BindView(R.id.textviewsearchincustomsmall)
    TextView textViewSearchInCustomSmall;

    //潜在客户，
    private ArrayList<EvaluteCurrentItemBean> mDataResources = new ArrayList<EvaluteCurrentItemBean>();

    //筛选条件的 汽车的类型；
    private ArrayList<NomalCustomBean> datalist_evaluate_type = new ArrayList<NomalCustomBean>();

    //该客户的分配状态
    private ArrayList<NomalCustomBean> datalist_evaluate_status = new ArrayList<NomalCustomBean>();

    //上传搜索的关键字的姓名信息,是全量搜索，全匹配才算一条有效搜索；
    private String keyWord_upLoad = "";
    //评估的类型
    private String estimateType_upLoad = "";
    //评估的状态
    private String status_upLoad = "";

    //适配器
    private EvaluateListItemAdapter evaluateListItemAdapter;

    /**
     * 最后一条上传的id的信息内容
     */
    private int pagination_upLoad = Parameterkey.pageNum_default_first;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivityInstance = this;

        mSession = Session.get(this);

        if (datalist_evaluate_type == null) {
            datalist_evaluate_type = new ArrayList<>();
        }

        if (datalist_evaluate_status == null) {
            datalist_evaluate_status = new ArrayList<>();
        }

        initloading();

        initViews();

        gainDatas();

        //监听单个客户内容
        //列表之内需要向外部广播数据
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();

        ImmersionBar.with(this)//
                .titleBar(R.id.toolbarygxskout)
                .fitsSystemWindows(true)//解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                //.statusBarColor(R.color.colorPrimary)//
                //.navigationBarColor(R.color.colorPrimary)//
                //.keyboardEnable(true)
                .init();

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_car_evaluate_main;
    }

    @Override
    protected void initToolBar(ToolbarHelper toolbarHelper) {

        toolbarHelper.setTitle(getString(R.string.carevaluatemainstring));
        toolbarHelper.setVersionName(true, AppUtils.getAppVersionName());
        toolbarHelper.setLayoutClose(false, "", null);
        toolbarHelper.setLayoutLeft(true, R.drawable.backicon, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //搜索客户信息，优先关闭软键盘
                KeyboardUtils.hideSoftInput(mActivityInstance);
                methodBack();
            }
        });

        //添加潜在客户的按钮
        toolbarHelper.setLayoutRight(true, 1, getString(R.string.title_right_history), new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {

                //快速双加的限制，
                Bundle extra_bundle = new Bundle();
                // : 2023-07-14 17:59  跳转到评估的历史记录内容
                ActivityUtils.startActivity(extra_bundle, CarEvaluateHistoryActivity.class);

            }
        });

    }

    @Override
    protected void onClickNodata(View v) {

        gainDatas();

    }

    @Override
    protected void initViews() {

        //搜索发生搜索的编辑框事件
        if (et_searchInCustomSmall != null) {

            et_searchInCustomSmall.setHint(StringUtils.getString(R.string.search_keyword_full_name_phone_evaluate));
            et_searchInCustomSmall.clearFocus();

            //这里没有列表数据，不清理列表；
//            et_searchInCustomSmall.setFocusable(true);
//            et_searchInCustomSmall.setFocusableInTouchMode(true);
//            et_searchInCustomSmall.requestFocus();

            this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

            //搜索事件
            //使用rxbinding做监听，如果1秒钟没有输入的话就自动清除焦点
//            RxTextView.afterTextChangeEvents(et_searchInCustom)
//                    .debounce(1000, TimeUnit.MILLISECONDS)//
//                    .skip(1)//过滤到第一个空字符的内容
//                    .subscribeOn(AndroidSchedulers.mainThread())//
//                    .observeOn(AndroidSchedulers.mainThread())//
//                    .subscribe(new Consumer<TextViewAfterTextChangeEvent>() {
//                        @Override
//                        public void accept(TextViewAfterTextChangeEvent textViewAfterTextChangeEvent) throws Throwable {
//
//                            //LogUtils.d(ConstantApi.LOGCAT_NOMAL + "监听到的内容是->" + textViewAfterTextChangeEvent.getView().getText().toString());
//                            if (et_searchInCustom.isFocused()) {
//                                //LogUtils.d(ConstantApi.LOGCAT_NOMAL + "afterTextChangeEvents--处于获取焦点状态");
//                                et_searchInCustom.clearFocus();//如果处于占用焦点之中，那么清理焦点
//                            }
//
//                        }
//                    }, new Consumer<Throwable>() {
//
//                        @Override
//                        public void accept(Throwable throwable) throws Throwable {
//                            //
//                        }
//                    });

            //软键盘的【搜索】
            et_searchInCustomSmall.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {

                    if (actionId == EditorInfo.IME_ACTION_SEARCH || (keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {

                        //关闭软键盘
                        KeyboardUtils.hideSoftInput(mActivityInstance);

                        method_SearchSomeThing();

                        return true;
                    }

                    return false;
                }
            });
        }

        if (textViewSearchInCustomSmall != null) {
            textViewSearchInCustomSmall.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    LogUtils.d(ConstantSign.LOGCAT_NOMAL + "点击布局");

                    if (KeyboardUtils.isSoftInputVisible(mActivityInstance)) {
                        KeyboardUtils.hideSoftInput(mActivityInstance);
                    }

                    method_SearchSomeThing();
                }
            });
        }

        //-------------------------------------------------------------------------------------------

        //全部 所有
        NomalCustomBean nomalCustomBeanAll = new NomalCustomBean();
        nomalCustomBeanAll.setDictCode(EvaluteCarEstimateType.ESTIMATE_TYPE_ALL.getIndex());
        nomalCustomBeanAll.setDictLabel(EvaluteCarEstimateType.ESTIMATE_TYPE_ALL.getValue());

        //在线评估
        NomalCustomBean nomalCustomBean0 = new NomalCustomBean();
        nomalCustomBean0.setDictCode(EvaluteCarEstimateType.ESTIMATE_TYPE_ONLINE.getIndex());
        nomalCustomBean0.setDictLabel(EvaluteCarEstimateType.ESTIMATE_TYPE_ONLINE.getValue());

        //预约评估
        NomalCustomBean nomalCustomBean1 = new NomalCustomBean();
        nomalCustomBean1.setDictCode(EvaluteCarEstimateType.ESTIMATE_TYPE_BOOKING.getIndex());
        nomalCustomBean1.setDictLabel(EvaluteCarEstimateType.ESTIMATE_TYPE_BOOKING.getValue());

        //上门评估
        NomalCustomBean nomalCustomBean2 = new NomalCustomBean();
        nomalCustomBean2.setDictCode(EvaluteCarEstimateType.ESTIMATE_TYPE_ONDOOR.getIndex());
        nomalCustomBean2.setDictLabel(EvaluteCarEstimateType.ESTIMATE_TYPE_ONDOOR.getValue());

        //到店评估
        NomalCustomBean nomalCustomBean3 = new NomalCustomBean();
        nomalCustomBean3.setDictCode(EvaluteCarEstimateType.ESTIMATE_TYPE_ONSTORE.getIndex());
        nomalCustomBean3.setDictLabel(EvaluteCarEstimateType.ESTIMATE_TYPE_ONSTORE.getValue());

        datalist_evaluate_type.add(nomalCustomBeanAll);
        datalist_evaluate_type.add(nomalCustomBean0);
        datalist_evaluate_type.add(nomalCustomBean1);
        datalist_evaluate_type.add(nomalCustomBean2);
        datalist_evaluate_type.add(nomalCustomBean3);

        //直接存储mmkv的信息
        MMKV.defaultMMKV().encode(ConstantSign.MMKV_JSON_NOMAL_CUSTOM_JSON_EVALUATE_CAR, JSON.toJSONString(datalist_evaluate_type));
        //初始化 选择类型
        method_spinnser_evaluateType();

        //----------------------------------------------------------------------------------

        NomalCustomBean nomalCustomBeanStateAll = new NomalCustomBean();
        nomalCustomBeanStateAll.setDictCode(EvaluteCarStatus.STATUS_ALL.getIndex());
        nomalCustomBeanStateAll.setDictLabel(EvaluteCarStatus.STATUS_ALL.getValue());

        NomalCustomBean nomalCustomBeanState1 = new NomalCustomBean();
        nomalCustomBeanState1.setDictCode(EvaluteCarStatus.STATUS_NOT_ASSIGNED.getIndex());
        nomalCustomBeanState1.setDictLabel(EvaluteCarStatus.STATUS_NOT_ASSIGNED.getValue());

        NomalCustomBean nomalCustomBeanState2 = new NomalCustomBean();
        nomalCustomBeanState2.setDictCode(EvaluteCarStatus.STATUS_HAS_ASSIGNED.getIndex());
        nomalCustomBeanState2.setDictLabel(EvaluteCarStatus.STATUS_HAS_ASSIGNED.getValue());

        // TODO: 2024/1/19 15:58 为啥不要了，我不知道原因了？
//        NomalCustomBean nomalCustomBeanState3 = new NomalCustomBean();
//        nomalCustomBeanState3.setDictCode(EvaluteCarStatus.STATUS_HAS_EVALUATED.getIndex());
//        nomalCustomBeanState3.setDictLabel(EvaluteCarStatus.STATUS_HAS_EVALUATED.getValue());

        datalist_evaluate_status.add(nomalCustomBeanStateAll);
        datalist_evaluate_status.add(nomalCustomBeanState1);
        datalist_evaluate_status.add(nomalCustomBeanState2);
//        datalist_evaluate_status.add(nomalCustomBeanState3);

        //直接存储mmkv的信息
        MMKV.defaultMMKV().encode(ConstantSign.MMKV_JSON_NOMAL_CUSTOM_JSON_EVALUATE_STATUS, JSON.toJSONString(datalist_evaluate_status));

        method_spinnser_stateItem();

        //-------------------------------------------------------------------------------------------
        //-------------------------------------------------------------------------------------------
        //-------------------------------------------------------------------------------------------
        //布局的适配器填充，下拉刷新等等；
        recyclerViewInEvaluateListActivity.setLayoutManager(new MyLinearLayoutManager(this));

        evaluateListItemAdapter = new EvaluateListItemAdapter(R.layout.evaluatelistadapter, mDataResources);
        recyclerViewInEvaluateListActivity.setAdapter(evaluateListItemAdapter);

        evaluateListItemAdapter.notifyDataSetChanged();

        evaluateListItemAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                EvaluteCurrentItemBean evaluteCurrentItemBean = (EvaluteCurrentItemBean) adapter.getItem(position);

                if (evaluteCurrentItemBean != null) {

                    //评估的类型：
                    String estimateType = evaluteCurrentItemBean.getEstimateType();

                    Bundle extra_bundle = new Bundle();

                    //不是编辑过去的：2023-8-26 11:00:20
                    //bug:2024年1月19日17:02:26  发现从这里进去是携带过去再次编辑，确实编辑的逻辑，不能携带空信息过去
                    extra_bundle.putBoolean(ExtraKey.EXTRA_EVALUATE_DETAIL_IS_EDIT, true);

                    // : 2023-07-17 18:11 在详情内获取到这个评估人员的状态；
                    if (TextUtils.equals(estimateType, EvaluteCarEstimateType.ESTIMATE_TYPE_BOOKING.getIndex())) {

                        extra_bundle.putString(ExtraKey.EXTRA_JUMPFROM_WHERE_TYPE, EvaluteCarEstimateType.ESTIMATE_TYPE_BOOKING.getIndex()); //预约评估
                        extra_bundle.putString(ExtraKey.EXTRA_EVALUATE_ADD_SUBMIT_TYPE, EvaluteCarEstimateType.ESTIMATE_TYPE_BOOKING.getValue());//
                        extra_bundle.putParcelable(ExtraKey.EXTRA_EVALUTE_CURRENT_ITEM_BEAN, evaluteCurrentItemBean);//

                        ActivityUtils.startActivity(extra_bundle, CarEvaluateAddSubmitActivity.class);

                    } else if (TextUtils.equals(estimateType, EvaluteCarEstimateType.ESTIMATE_TYPE_ONSTORE.getIndex())) {

                        extra_bundle.putString(ExtraKey.EXTRA_JUMPFROM_WHERE_TYPE, EvaluteCarEstimateType.ESTIMATE_TYPE_ONSTORE.getIndex()); //到店评估--是直接新增的一个评估。
                        extra_bundle.putString(ExtraKey.EXTRA_EVALUATE_ADD_SUBMIT_TYPE, EvaluteCarEstimateType.ESTIMATE_TYPE_ONSTORE.getValue());//
                        extra_bundle.putParcelable(ExtraKey.EXTRA_EVALUTE_CURRENT_ITEM_BEAN, evaluteCurrentItemBean);//

                        ActivityUtils.startActivity(extra_bundle, CarEvaluateAddSubmitActivity.class);

                    } else if (TextUtils.equals(estimateType, EvaluteCarEstimateType.ESTIMATE_TYPE_ONDOOR.getIndex())) {

                        extra_bundle.putString(ExtraKey.EXTRA_JUMPFROM_WHERE_TYPE, EvaluteCarEstimateType.ESTIMATE_TYPE_ONDOOR.getIndex()); //上门评估
                        extra_bundle.putString(ExtraKey.EXTRA_EVALUATE_ADD_SUBMIT_TYPE, EvaluteCarEstimateType.ESTIMATE_TYPE_ONDOOR.getValue());//
                        extra_bundle.putParcelable(ExtraKey.EXTRA_EVALUTE_CURRENT_ITEM_BEAN, evaluteCurrentItemBean);//

                        ActivityUtils.startActivity(extra_bundle, CarEvaluateAddSubmitActivity.class);

                    } else if (TextUtils.equals(estimateType, EvaluteCarEstimateType.ESTIMATE_TYPE_ONLINE.getIndex())) {

                        extra_bundle.putString(ExtraKey.EXTRA_JUMPFROM_WHERE_TYPE, EvaluteCarEstimateType.ESTIMATE_TYPE_ONLINE.getIndex()); //在线评估
                        extra_bundle.putString(ExtraKey.EXTRA_EVALUATE_ADD_SUBMIT_TYPE, EvaluteCarEstimateType.ESTIMATE_TYPE_ONLINE.getValue());//
                        extra_bundle.putParcelable(ExtraKey.EXTRA_EVALUTE_CURRENT_ITEM_BEAN, evaluteCurrentItemBean);//

                        ActivityUtils.startActivity(extra_bundle, CarEvaluateAddSubmitActivity.class);

                    } else {
                        //如果不是以上几种，那么默认认为是：到店评估
                        extra_bundle.putString(ExtraKey.EXTRA_JUMPFROM_WHERE_TYPE, EvaluteCarEstimateType.ESTIMATE_TYPE_ONSTORE.getIndex()); //到店评估
                        extra_bundle.putString(ExtraKey.EXTRA_EVALUATE_ADD_SUBMIT_TYPE, EvaluteCarEstimateType.ESTIMATE_TYPE_ONSTORE.getValue());//
                        extra_bundle.putParcelable(ExtraKey.EXTRA_EVALUTE_CURRENT_ITEM_BEAN, evaluteCurrentItemBean);//

                        ActivityUtils.startActivity(extra_bundle, CarEvaluateAddSubmitActivity.class);
                    }
                }
            }
        });

        evaluateListItemAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                EvaluteCurrentItemBean evaluteCurrentItemBean = (EvaluteCurrentItemBean) adapter.getItem(position);

                if (evaluteCurrentItemBean != null) {

                    switch (view.getId()) {

                        case R.id.textvieweditviewphoneevaluate: {

                            clickButton_toCallPhone(CarEvaluateMainActivity.this, evaluteCurrentItemBean.getCustomerPhone(), CallHistoryBusinessType.BUSINESSTYPE_4.getIndex());

                            break;
                        }

                        default:
                            break;
                    }

                }
            }
        });

        refreshLayoutInEvaluateListActiviy.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                refreshLayout.getLayout().post(new Runnable() {
                    @Override
                    public void run() {

                        if (mDataResources != null && !mDataResources.isEmpty()) {
                            mDataResources.clear();
                        }

                        if (evaluateListItemAdapter != null) {
                            evaluateListItemAdapter.notifyDataSetChanged();
                        }

                        // : 2023-07-13 15:11  待处理 筛选状态
                        estimateType_upLoad = "";
                        nice_spinner_evaluate_type.setText(StringUtils.getString(R.string.please_evaluate_cartype));

                        // : 2023-08-09 19:26 待处理 选择状态
                        status_upLoad = "";
                        nice_spinner_state_item.setText(getString(R.string.please_choose_state_assigned));

                        // : 2023-08-09 19:26 待处理搜索的关键字
                        keyWord_upLoad = "";
                        et_searchInCustomSmall.setText("");

                        pagination_upLoad = Parameterkey.pageNum_default_first;

                        getServerDatas();

                        refreshLayout.finishRefresh();
                    }
                });

            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

                refreshLayout.getLayout().post(new Runnable() {
                    @Override
                    public void run() {

                        //最后一条数据的id信息；
                        getServerDatas();

                        refreshLayout.finishLoadMore();

                    }
                });
            }
        });

        //跳转到添加评估页面
        if (roundButtoAddNewEvaluate != null) {
            roundButtoAddNewEvaluate.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                @Override
                public void onDebouncingClick(View v) {

                    Bundle extra_bundle = new Bundle();

//                    //String shopId = assetInSideBean.getShopId();
//                    //是添加进来的
//                    extra_bundle.putInt(ExtraKey.EXTRA_JUMPFROM_WHERE,ExtraValue.VALUE_JUMP_FORM_ASSET_ADD);
//                    //
                    //这个携带很关键，是新添加一个；评估信息，那么是没有【分配客户】按钮的
                    //不是编辑过去的：2023-8-26 11:00:20
                    extra_bundle.putBoolean(ExtraKey.EXTRA_EVALUATE_DETAIL_IS_EDIT, false);
                    //标志一个是新添加：【车辆评估】
                    extra_bundle.putString(ExtraKey.EXTRA_JUMPFROM_WHERE_TYPE, EvaluteCarEstimateType.ESTIMATE_TYPE_NEW_ADD_ONE.getIndex()); //是新增一个评估
                    extra_bundle.putString(ExtraKey.EXTRA_EVALUATE_ADD_SUBMIT_TYPE, EvaluteCarEstimateType.ESTIMATE_TYPE_NEW_ADD_ONE.getValue());
                    ActivityUtils.startActivity(extra_bundle, CarEvaluateAddSubmitActivity.class);

                }
            });
        }

    }

    private void method_SearchSomeThing() {

        //获取搜索的关键字内容,刚开始默认是空值
        keyWord_upLoad = et_searchInCustomSmall.getText().toString().trim();

//        if (TextUtils.isEmpty(et_searchInCustomSmall.getText().toString().trim())) {
//            //ToastUtils.make().setGravity(Gravity.CENTER, 0, 0);
//            //ToastUtils.showShort("请输入搜索关键字");
//            CommUtils.checkDialog(mAlertView);
//            mAlertView = CommUtils.method_showAlertViewSingleSlowly(mActivityInstance, ConstantSign.ALERT_TITLE, StringUtils.getString(R.string.pleaseinputkeyword), true);
//            mAlertView.show();
//            //不发生搜索
//            return;
//        }

        if (!TextUtils.isEmpty(keyWord_upLoad)) {

            if ((keyWord_upLoad).length() < Parameterkey.keyWordLength) {
                //ToastUtils.showShort("最少输入两个字符！");
                CommUtils.checkDialog(mAlertView);
                mAlertView = CommUtils.method_showAlertViewSingleSlowly(CarEvaluateMainActivity.this, ConstantSign.ALERT_TITLE, StringUtils.getString(R.string.pleaseinputkeywordtwo), true);
                mAlertView.show();
                return;
            }

            pagination_upLoad = Parameterkey.pageNum_default_first;

            if (mDataResources != null && !mDataResources.isEmpty()) {
                mDataResources.clear();
            }

            if (evaluateListItemAdapter != null) {
                evaluateListItemAdapter.notifyDataSetChanged();
            }

            getServerDatas();

        } else {

            //如果是 空关键字，不发生搜索；
            //2023年5月9日15:23:21 发现搜索完毕，又紧接着弹出了软键盘；
            et_searchInCustomSmall.clearFocus();

            if (TextUtils.isEmpty(keyWord_upLoad)) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0);
                ToastUtils.showLong(getString(R.string.pleaseinputkeyword));
            }

        }
    }

    private void method_spinnser_evaluateType() {

        //默认原始的是【请选择】接口请求完毕，默认填充第一个：目的是：有一个默认的文字的
        nice_spinner_evaluate_type.setText(StringUtils.getString(R.string.please_evaluate_cartype));
        //设置选中文字？
//        nice_spinner_cartype.setTextColor(ColorUtils.getColor(R.color.white));
        //仅仅有点击事件
        nice_spinner_evaluate_type.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {

                KeyboardUtils.hideSoftInput(CarEvaluateMainActivity.this);

                //类型是否是动态的？
                if (datalist_evaluate_type != null && !datalist_evaluate_type.isEmpty()) {

                    //添加 评估的类别类型；
                    method_carType_fragmentDialog();

                } else {
                    // : 2023-07-13 17:51 是否从接口获取 ；
                }
            }
        });
    }

    private void method_spinnser_stateItem() {

        //默认原始的是【请选择】接口请求完毕，默认填充第一个：目的是：有一个默认的文字的
        nice_spinner_state_item.setText(StringUtils.getString(R.string.please_choose_state_assigned));
        //设置选中文字？
//        nice_spinner_cartype.setTextColor(ColorUtils.getColor(R.color.white));

        //仅仅有点击事件
        nice_spinner_state_item.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {

                KeyboardUtils.hideSoftInput(CarEvaluateMainActivity.this);

                //类型是否是动态的？
                if (datalist_evaluate_status != null && !datalist_evaluate_status.isEmpty()) {

                    //添加 分配状态
                    method_state_assigned_fragmentDialog();

                } else {

                    // : 2023-07-13 17:51 是否从接口获取 ；

                }

            }

        });


    }

    //汽车的评估类型类型 弹窗；
    private void method_carType_fragmentDialog() {

        //汽车的类型 弹窗；
        NomalPickerDialogFragment nomalPickerDialogFragment = NomalPickerDialogFragment.newInstance(
                getResources().getString(R.string.please_evaluate_cartype),//
                datalist_evaluate_type,//
                ConstantApi.INDEXFLAG_EVALUATE_CARTYPE);//

        nomalPickerDialogFragment.setOnNomalChooseListener(new NomalPickerDialogFragment.OnNomalChooseListener() {
            @Override
            public void onNomalChoose(NomalCustomBean nomalCustomBean) {

                LogUtils.json(ConstantSign.LOGCAT_NOMAL + "选中的内容是-->", nomalCustomBean);

                if (nomalCustomBean != null) {

                    estimateType_upLoad = nomalCustomBean.getDictCode();

                    nice_spinner_evaluate_type.post(new Runnable() {
                        @Override
                        public void run() {
                            nice_spinner_evaluate_type.setText(nomalCustomBean.getDictLabel());
                        }
                    });

                    method_clear_datas();

                    //选中之后，更改标题；
                    //-----------------------------------------------------------------------------------
                    //重新存入json
                    MMKV.defaultMMKV().encode(ConstantSign.MMKV_JSON_NOMAL_CUSTOM_JSON, JSON.toJSONString(ViewUitls.method_sort(datalist_evaluate_type, nomalCustomBean)));

                } else {

                    //重置清空 条件；
                    estimateType_upLoad = "";
                    nice_spinner_evaluate_type.post(new Runnable() {
                        @Override
                        public void run() {
                            nice_spinner_evaluate_type.setText(StringUtils.getString(R.string.please_evaluate_cartype));
                        }
                    });

                    method_clear_datas();

                }

            }
        });

        nomalPickerDialogFragment.show(getSupportFragmentManager(), "NomalPickerDialogFragmentEvaluateType");

    }

    private void method_state_assigned_fragmentDialog() {

        NomalPickerDialogFragment nomalPickerDialogFragment = NomalPickerDialogFragment.newInstance(//
                getResources().getString(R.string.please_choose_state_assigned),//
                datalist_evaluate_status, ConstantApi.INDEXFLAG_EVALUATE_CAR_STATUS);//
        nomalPickerDialogFragment.setOnNomalChooseListener(new NomalPickerDialogFragment.OnNomalChooseListener() {
            @Override
            public void onNomalChoose(NomalCustomBean nomalCustomBean) {

                if (nomalCustomBean != null) {

                    // : 2023-03-31 做下拉选择的监听--取出用于上传的 检查方式；
                    status_upLoad = nomalCustomBean.getDictCode();

                    //变动spinner的文本描述；
                    nice_spinner_state_item.post(new Runnable() {
                        @Override
                        public void run() {
                            nice_spinner_state_item.setText(nomalCustomBean.getDictLabel());
                        }
                    });

                    method_clear_datas();

                    //----------------------------------------------------------------------
                    //重新保存json
                    MMKV.defaultMMKV().encode(ConstantSign.MMKV_JSON_NOMAL_CUSTOM_JSON_EVALUATE_STATUS, JSON.toJSONString(ViewUitls.method_sort(datalist_evaluate_status, nomalCustomBean)));
                    //-----------------------------------------------------------------------------------------------------------------------------------------------------

                } else {

                    //是 清空重置
                    status_upLoad = "";
                    nice_spinner_state_item.post(new Runnable() {
                        @Override
                        public void run() {
                            nice_spinner_state_item.setText(getString(R.string.please_choose_state_assigned));
                        }
                    });

                    method_clear_datas();

                }
            }
        });

        nomalPickerDialogFragment.show(getSupportFragmentManager(), "NomalPickerDialogFragment");

    }

    //清理数据，用于顶部条件筛选切换
    private void method_clear_datas() {

        pagination_upLoad = Parameterkey.pageNum_default_first;//重置

        if (mDataResources != null && !mDataResources.isEmpty()) {
            mDataResources.clear();
        }

        if (evaluateListItemAdapter != null) {
            evaluateListItemAdapter.notifyDataSetChanged();
        }

        getServerDatas();

    }

    @Override
    protected void gainDatas() {

        //是否有其他需要调用接口的地方；
        //这个接口内就是刷新全部内容
        pagination_upLoad = Parameterkey.pageNum_default_first;//重置

        if (mDataResources != null && !mDataResources.isEmpty()) {
            mDataResources.clear();
        }

        if (evaluateListItemAdapter != null) {
            evaluateListItemAdapter.notifyDataSetChanged();
        }

        if (NetworkUtils.isConnected()) {

            getServerDatas();

        } else {
            //是否有其他的执行逻辑？
            netWorkError();
            setEnableLoadmore(refreshLayoutInEvaluateListActiviy, false);

        }

    }

    private void getServerDatas() {

        keyWord_upLoad = et_searchInCustomSmall.getText().toString();

        HashMap<String, Object> map = new HashMap<>();
        map.put(Parameterkey.pagination, pagination_upLoad);//该第几页
        map.put(Parameterkey.pageSize, Parameterkey.pageSize_default);//一页多少条
        map.put(Parameterkey.keyWord, keyWord_upLoad);//,关键字信息
        map.put(Parameterkey.shopInfoId, mSession.getShopInfoId());//店铺信息
        map.put(Parameterkey.estimateType, estimateType_upLoad);//评估的类型
        map.put(Parameterkey.status, status_upLoad);//评估的状态

//                EvaluteCarStatus
//                STATUS_NOT_ASSIGNED("0", "待分配"),
//                STATUS_HAS_ASSIGNED("1", "已分配"),
//                STATUS_HAS_EVALUATED("2", "已评估");

        //这里是为了过滤掉【已评估】；只筛选【待分配】+【已分配】的；
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer
                .append(EvaluteCarStatus.STATUS_NOT_ASSIGNED.getIndex().trim())//
                .append(",")//
                .append(EvaluteCarStatus.STATUS_HAS_ASSIGNED.getIndex().trim());//

        map.put(Parameterkey.statusStr, stringBuffer.toString());//评估的状态

        AddLogUpLoadInfo addLogUpLoadInfo = new AddLogUpLoadInfo();
        addLogUpLoadInfo.setActivity(mActivityInstance);
        addLogUpLoadInfo.setWhat(ConstantApi.WHAT_GETESTIMATELISTBYPAGE_URL);//请求数字标识
        addLogUpLoadInfo.setUrlPath(ApiService.getEstimateListByPage_url);//url地址
        addLogUpLoadInfo.setRequestMethod(ConstantSign.REQUEST_METHOD_GET);//POST 或者 GET
        addLogUpLoadInfo.setToastToUser(true);//展示给用户的标志
        addLogUpLoadInfo.setHashMapParameter(map);

        NetworkManager.getInstance().initRetrofitRxJava()
                .create(ApiService.class)
                .getEstimateListByPage(RequestBody.create(JSON.toJSONString(map,//
                                SerializerFeature.PrettyFormat,//
                                SerializerFeature.WriteMapNullValue), //
                        MediaType.parse(ApiService.HEADER_JSON)))//
                .compose(CarEvaluateMainActivity.this.bindUntilEvent(ActivityEvent.DESTROY))//
                .compose(new ResponseTransformer<>(addLogUpLoadInfo, new ApiRequestSubListener<EvaluteCurrentItemOutSide>() {
                    @Override
                    public void handlerSomeThingNotSuccessDataForItSelf(int what_method, String serverCode, String serverMessage) {
                        super.handlerSomeThingNotSuccessDataForItSelf(what_method, serverCode, serverMessage);
//                        methodNotSuccessData(what_method, serverCode, serverMessage);
                        if (mDataResources != null && mDataResources.isEmpty()) {

                            if (refreshLayoutInEvaluateListActiviy != null) {
                                refreshLayoutInEvaluateListActiviy.setEnableLoadMore(false);
                            }

                            CommUtils.checkCurrently((ErrorActivity) mActivityInstance,//
                                    serverCode,//
                                    R.drawable.errorsear,//
                                    serverMessage,//
                                    getString(R.string.nulldatanow));
                        }
                    }
                })).doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Throwable {

                        loadingGone();

                        if (refreshLayoutInEvaluateListActiviy != null) {
                            refreshLayoutInEvaluateListActiviy.setEnableLoadMore(true);
                        }

                    }
                }).subscribe(new Consumer<EvaluteCurrentItemOutSide>() {
                    @Override
                    public void accept(EvaluteCurrentItemOutSide evaluteCurrentItemOutSide) throws Throwable {

                        if (evaluteCurrentItemOutSide != null) {

                            //因为有加载更多不能清空
//                            if (mDataResources != null && !mDataResources.isEmpty()) {
//                                mDataResources.clear();
//                            }

                            if (evaluteCurrentItemOutSide.getDataList() != null && !evaluteCurrentItemOutSide.getDataList().isEmpty()) {
                                if (mDataResources != null) {
                                    mDataResources.addAll(evaluteCurrentItemOutSide.getDataList());
                                }
                            }

                            pagination_upLoad = evaluteCurrentItemOutSide.getNextPagination();

                            //对列表改造：
                            if (pagination_upLoad == Parameterkey.GET_NEXT_PAGINATION) {
                                setFinishLoadMoreWithNoMoreData(refreshLayoutInEvaluateListActiviy, true);
                            } else {
                                setEnableLoadmore(refreshLayoutInEvaluateListActiviy, true);
                            }

                        }

                        //这里也不用管是下拉刷新或者上拉加载更多，直接判断是否为空，
                        if (mDataResources != null && mDataResources.isEmpty()) {
                            //同时禁用加载更多；
                            if (refreshLayoutInEvaluateListActiviy != null) {
                                refreshLayoutInEvaluateListActiviy.setEnableLoadMore(false);
                            }
                            fillNullDataView(StringUtils.getString(R.string.nulldatanow), R.drawable.errorsear);
                        }

                        if (evaluateListItemAdapter != null) {
                            evaluateListItemAdapter.notifyDataSetChanged();
                        }

                    }
                }, new ErrorConsumer() {

                    @Override
                    protected void error(ApiException apiException) {

                        //页面报错；
                        if (refreshLayoutInEvaluateListActiviy != null) {
                            refreshLayoutInEvaluateListActiviy.setEnableLoadMore(false);
                        }
                        //fillNullDataView(StringUtils.getString(R.string.nulldatanow), R.drawable.errorsear);
                    }
                });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThreadInEvaluateCar(EventCarEvaluateMain eventCarEvaluateMain) {

        if (eventCarEvaluateMain != null) {

            if (eventCarEvaluateMain.getMessage() == ExtraValue.EVENTBUS_EVENT_TO_REFRESH_EVALUATE_CAR_LIST) {

                //如果发起搜索，要重置 第1页
                pagination_upLoad = Parameterkey.pageNum_default_first;

                //清空历史数据
                if (mDataResources != null && !mDataResources.isEmpty()) {
                    mDataResources.clear();
                }
//                if (workDailyHistoryAdapter != null) {
//                    workDailyHistoryAdapter.notifyDataSetChanged();
//                }

                getServerDatas();

            }

        }

    }

    @Override
    protected void methodBack() {

        if (ActivityUtils.isActivityAlive(this)) {
            ActivityUtils.finishActivity(this);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mDataResources != null && !mDataResources.isEmpty()) {
            mDataResources.clear();
        }

        if (evaluateListItemAdapter != null) {
            evaluateListItemAdapter.notifyDataSetChanged();
            evaluateListItemAdapter = null;
        }

        // : 2023-07-14 18:01 释放内存
        EventBus.getDefault().unregister(this);

    }
}